using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Garlic.CC.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Garlic.DM.DataModel;

namespace Garlic.DAL
{
	/// <summary>
	/// DataAccess
	/// </summary>
	public partial class AuthDAL
	{
		#region Sql const
		public  const string TS_BAS_AUTH_SELECT_BY_ID=" SELECT role_id,resource_type,resource_name,resource_id"+
											" FROM TS_BAS_Auth"+
											" WHERE 1=1 " +
											" and  role_id =  :role_id";

											
		public  const string TS_BAS_AUTH_SELECT=" SELECT "+
											"role_id,"+
											"resource_type,"+
											"resource_name,"+
											"resource_id"+
											" FROM TS_BAS_Auth {0}";
														
								
	
		public  const string TS_BAS_AUTH_INSERT=" INSERT INTO TS_BAS_Auth ("+
											"role_id,"+
											"resource_type,"+
											"resource_name,"+
											"resource_id"+
										") VALUES ("+
											":role_id,"+
											":resource_type,"+
											":resource_name,"+
											":resource_id"+
										")";
									
									
		public  const string TS_BAS_AUTH_UPDATE=" UPDATE TS_BAS_Auth SET "+
											" resource_type =  :resource_type, "+
											" resource_name =  :resource_name, "+
											" resource_id =  :resource_id "+
										" WHERE 1=1 " +
											" and  role_id =  :role_id";
										
	
	
		public  const string TS_BAS_AUTH_DELETE=" DELETE FROM TS_BAS_Auth" +
										" WHERE 1=1"+
											" and  role_id =  :role_id";
										
										
		
		#endregion
		
		/// <summary>
		/// Get AuthInfo by role_id
		/// </summary>
		/// <param name="role_id">AuthInfo </param>
		/// <returns></returns>
		public static AuthInfo GetInfo(int role_id)
		{
	
				Database db = DatabaseFactory.CreateDatabase();
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_AUTH_SELECT_BY_ID);
			db.AddInParameter(dbCommand, "role_id", DbType.Int32,role_id);
				

				using (IDataReader dr = db.ExecuteReader(dbCommand))
				{
					if (dr.Read())
					{
						return CreateAuthInfo(dr);
					}
					else
						return null;
				}
		}

        public static void UpdaetToUser(int menuid, int userid, int is_effective)
        {
            string sql = @" UPDATE [psprd].[TS_BAS_Auth_User] SET [is_effective] = " + is_effective + " where [user_id] = " + userid + " and resource_id = " + menuid + " and  resource_type='Menu'";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            db.ExecuteNonQuery(dbCommand);
        }
	
		/// <summary>
		/// Get data list
		/// </summary>
		/// <param name="textWhere">conditon</param>
		/// <param name="orderText">sort</param>
		/// <returns>AuthInfo list </returns>
		public static List<AuthInfo> GetList(string textWhere,string orderText)
		{
			string query=string.Empty;
			if(!string.IsNullOrEmpty(textWhere))
			{
			query+=" where "+textWhere; 
			}
			
			if(!string.IsNullOrEmpty(orderText))
			{
			query+=" order by "+orderText;
			}
			
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(string.Format(TS_BAS_AUTH_SELECT,query));
			
			return GetList(string.Format(TS_BAS_AUTH_SELECT,query));
			
		}
		
		/// <summary>
		/// Get data list
		/// </summary>
		/// <param name="sql">SQL statement</param>
		/// <returns>AuthInfo list </returns>
		public static List<AuthInfo> GetList(string sql)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(sql);
			List<AuthInfo> list=new List<AuthInfo>();
			using (IDataReader dr = db.ExecuteReader(dbCommand))
			{
				while(dr.Read())
				{
					list.Add(CreateAuthInfo(dr));
				}
			}
			
			return list;
		}

	
		/// <summary>
		/// Add
		/// </summary>
		/// <param name="authInfo"> entity</param>
		public static int Add(AuthInfo authInfo)
		{
				Database db = DatabaseFactory.CreateDatabase();
	
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_AUTH_INSERT);
				
				db.AddInParameter(dbCommand, "role_id",DbType.Int32,authInfo.Role_id); 
				db.AddInParameter(dbCommand, "resource_type",DbType.AnsiString,DBConvert.ToDBValue(authInfo.Resource_type)); 
				db.AddInParameter(dbCommand, "resource_name",DbType.AnsiString,DBConvert.ToDBValue(authInfo.Resource_name)); 
				db.AddInParameter(dbCommand, "resource_id",DbType.Int32,DBConvert.ToDBValue(authInfo.Resource_id)); 
				
				return int.Parse("0"+db.ExecuteScalar(dbCommand));
		}
		
		/// <summary>
		/// Update
		/// </summary>
		/// <param name="authInfo"> utility</param>
		public static void Update(AuthInfo authInfo)
		{
			Database db = DatabaseFactory.CreateDatabase();
	
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_AUTH_UPDATE);
				
				db.AddInParameter(dbCommand, "role_id",DbType.Int32,authInfo.Role_id); 
				db.AddInParameter(dbCommand, "resource_type",DbType.AnsiString,DBConvert.ToDBValue(authInfo.Resource_type)); 
				db.AddInParameter(dbCommand, "resource_name",DbType.AnsiString,DBConvert.ToDBValue(authInfo.Resource_name)); 
				db.AddInParameter(dbCommand, "resource_id",DbType.Int32,DBConvert.ToDBValue(authInfo.Resource_id)); 
				
				db.ExecuteNonQuery(dbCommand);
		}
		
		/// <summary>
		/// Delete
		/// </summary>
		/// <param name="role_id">TS_BAS_Auth Number</param>
		/// <returns></returns>
		public static void Delete(int role_id)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_AUTH_DELETE);
			db.AddInParameter(dbCommand, "role_id", DbType.Int32,role_id);
			db.ExecuteNonQuery(dbCommand);
		}
		
	

	
	}
}
